package a.a.a.filesystem;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

@Repository
public class GroupRepository {

	@Value("${path.root}")
	private String path;
	
	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	
	public boolean createGroup(String name) {
		if (!groupExists(name)) {
			File file = new File(path + name);
			return file.mkdir();
		}
		return false;
	}
	
	public boolean groupExists(String name) {
		File file = new File(path + name);
		return (file.exists() && file.isDirectory());
	}
	
	public List<String> getGroups() {
		List<String> groups = new ArrayList<String>();
		for (File f : new File(path).listFiles()) {
			if (f.isDirectory()) {
				groups.add(f.getName());
			}
		}
		return groups;
	}
}
